<?php
namespace app\controller;

use app\model\UsersModel;
use project\session;

class UsersControllers
{
  // private $controller;
  // private $action;
  const VIEW_PATH = APP_PATH . 'app/views/Users/';
    // protected $error = '';

  /**
   * 构造函数
   *
   * 开启会话，读取或存储数据
   */
  public function __construct()
  {
    session_start();
  }

  /**
   * 登录
   */
  public function loginAction()
  {
    // $this->renderAction();

    if (!isset($_SESSION["username"])) {
      $error = '';
      $title = '登录页面';
      include static::VIEW_PATH . 'login.php';
    } else {
      $this->homeAction();
    }
  }

  /**
   * 登录后显示主页
   *
   */
  public function homeAction()
  {
    $error = '';
    $name = 'home';
    $param = array();
    $title = "用户界面";
    $haveTask = false;

    // session_start();
    if (!isset($_SESSION["username"])) {
    //判断输入框
      if ($_POST['username'] === '' || $_POST['password'] === '') {
        $error = "账户或密码不能为空";
        $name = 'login';
        $title = "登录界面";
        include static::VIEW_PATH . $name . '.php';
        return;
      } else {
      //查询数据库，检测是否登陆成功
        $param['username'] = $_POST['username'];
        $param['password'] = md5($_POST['password']);
        $loginResult = (new UsersModel())->checkLogin($param);
        if ($loginResult === '') {
          $error = "账户或密码错误";
          $name = 'login';
          $title = "登录界面";
          include static::VIEW_PATH . $name . '.php';
          return;
        } else {
          $username = $loginResult[0][1];
          $_SESSION["username"] = $username;
        }
      }
    } else {
      $username = $_SESSION["username"];
    }
    //判断该用户是否有任务在身
    $dataResult = (new UsersModel())->showInfo($username);
    // var_dump($dataResult);
    if ($dataResult !== '') {
      foreach ($dataResult as $key => $value) {
        if ($value[1] === $username) {
          if ($value[3] === "0") {
            $haveTask = true;
            break;
          }
        }
      }
    }else{
      $haveTask = false;
    }
   
    $_SESSION['canAccept'] = !$haveTask;

    include static::VIEW_PATH . $name . '.php';
  }


  /**
   * 注销账户
   */
  public function logOutAction()
  {

    unset($_SESSION["username"]);
    unset($_SESSION["canAccept"]);
    $error = '';
    $title = '登录页面';
    include static::VIEW_PATH . 'login.php';
  }

  /**
   * 注册用户页面
   */
  public function registerAction($error ='')
  {
    $title = '登录页面';
    include static::VIEW_PATH . 'register.php';
  }


  /**
   * 注册用户
   */
  public function registerResultAction()
  {
    $title = "用户界面";
    $username = $_POST['username'];
    $password = md5($_POST['password'] );
    $result = (new UsersModel())->checkName($username);
    if ($result === '') {
      $result = (new UsersModel())->register($username,$password);
      if ($result === true) {
        $_SESSION["username"] = $username;
        $this->homeAction();
        return;
      }
      else{
        $error = "未知错误,请稍后再试";
      $this->registerAction($error);
      return;
      }
    } else {
      $error = '用户名已经被使用';
      $this->registerAction($error);
      return;
    }
  }
}